package com.contract.spirit.func.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.contract.spirit.R;

import java.util.ArrayList;
import java.util.List;


/**
 * @author zhengjun
 * @desc 支付密码输入框
 * @create at 2019/6/12 20:21
 */
public class PayPasswordInputView extends RelativeLayout {
    private final Context context;
    private final List<TextView> codeViews = new ArrayList<>();
    private EditText et_code;
    private final List<String> codes = new ArrayList<>();

    public PayPasswordInputView(Context context) {
        super(context);
        this.context = context;
        loadView();
    }

    public PayPasswordInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        parseAttrs(context, attrs);

    }

    private void parseAttrs(Context context, AttributeSet attrs) {

        loadView();
        @SuppressLint("CustomViewStyleable") TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.InputEdit, 0, 0);
        boolean isPassWord = typedArray.getBoolean(R.styleable.InputEdit_isPassWord, false);
        Drawable bgDrawble = typedArray.getDrawable(R.styleable.InputEdit_inputBg);
        int inputType = typedArray.getInteger(R.styleable.InputEdit_customInputType, InputType.TYPE_CLASS_NUMBER);
        for (int i = 0; i < codeViews.size(); i++) {
            TextView textView = codeViews.get(i);
            textView.setBackground(bgDrawble);
            textView.setInputType(inputType);
            if (isPassWord) {
                textView.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
        }
        typedArray.recycle();

    }

    private void loadView() {
        LayoutInflater.from(context).inflate(R.layout.view_input_pay_password_code, this, true);
        initView();
        initEvent();
    }

    private void initView() {
        codeViews.add(findViewById(R.id.tv_code1));
        codeViews.add(findViewById(R.id.tv_code2));
        codeViews.add(findViewById(R.id.tv_code3));
        codeViews.add(findViewById(R.id.tv_code4));
        codeViews.add(findViewById(R.id.tv_code5));
        codeViews.add(findViewById(R.id.tv_code6));
        et_code = findViewById(R.id.et_code);
    }

    public EditText getEt_code() {
        return et_code;
    }

    private void initEvent() {
        //验证码输入
        et_code.requestFocus();
        et_code.setFocusable(true);
        et_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable != null && editable.length() > 0) {
                    String value = editable.toString();
                    codes.clear();
                    for (int i = 0; i < value.length(); i++) {
                        String substring = value.substring(i,i+1);
                        codes.add(substring);
                    }
                    showCode(true);
                }else {
                    clearInputNumber();
                }
            }
        });
    }

    //点击自定义键盘删除按键
    public void remove() {
        if (codes.size() > 0) {
            codes.remove(codes.size() - 1);
            showCode(true);
        }
    }

    /**
     * 显示输入的验证码
     */
    private void showCode(boolean isCallBack) {
        for (int i = 0; i < codeViews.size(); i++) {
            TextView tvCode = codeViews.get(i);
            if (i < codes.size()) {
                String code = codes.get(i);
                tvCode.setText(code);
            } else {
                tvCode.setText("");
            }
        }
        if (isCallBack)
            callBack();//回调
    }

    /**
     * 回调
     */
    private void callBack() {
        if (onInputListener == null) {
            return;
        }
        if (codes.size() == 6) {
            KeyboardUtils.hideSoftInput(et_code);
            onInputListener.onSucess(getInputNumber());
        } else {
            onInputListener.onInput();
        }
    }

    //定义回调
    public interface OnInputListener {
        void onSucess(String code);

        void onInput();
    }

    private OnInputListener onInputListener;

    public void setOnInputListener(OnInputListener onInputListener) {
        this.onInputListener = onInputListener;
    }

    /**
     * 获取输入的密码
     *
     * @return 密码
     */
    private String getInputNumber() {
        StringBuilder sb = new StringBuilder();
        for (String code : codes) {
            sb.append(code);
        }
        return sb.toString();
    }

    //设置已经输入过的数字
    public void setInputNumber(String number) {
        char[] chars = number.toCharArray();
        for (char aChar : chars) {
            codes.add(aChar + "");
        }
        showCode(false);
    }

    //清空输入的数字
    public void clearInputNumber() {
        codes.clear();
        for (int i = 0; i < codeViews.size(); i++) {
            TextView textView = codeViews.get(i);
            textView.setText("");
        }
    }
}